/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.PayBillBean;
import com.minierp.bean.PayBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.PayBillDao;
import com.minierp.dao.PayBillItemDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.CooperBill;
import com.minierp.model.CooperBillExt;
import com.minierp.model.Establishment;
import com.minierp.model.FinanceDetail;
import com.minierp.model.PayBill;
import com.minierp.model.PayBillItem;
import com.minierp.model.PurchaseBill;
import com.minierp.model.PurchaseBillExt;
import com.minierp.model.SystemLog;
import com.minierp.service.PayBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PayBillServiceImpl implements PayBillService {
	public void txInsert(PayBillBean form, PayBillDao payBillDao,
			PayBillItemDao payBillItemDao, SerialNumberDao serialNumberDao,
			IdGenerator idGenerator, AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao) throws Exception {
		PayBill obj = form.getPayBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(PayBill.class.getName()));
		String payTypeName = "";
		if (obj.getPayType().equals("PURCHASE")) {
			payTypeName = "采购单";
		} else if (obj.getPayType().equals("COOPER")) {
			payTypeName = "外协单";
		}

		List list = form.getItems();

		for (int updateRelBillCost = 0; updateRelBillCost < list.size(); ++updateRelBillCost) {
			PayBillItemBean costType = (PayBillItemBean) list
					.get(updateRelBillCost);
			PayBillItem extBillType = costType.getPayBillItem();
			if (!costType.getStatus().equals("DELETE")
					&& (new BigDecimal(costType.getThisCostStr()))
							.compareTo(new BigDecimal(0)) != 0) {
				List financeType = payBillDao.getList(obj.getPayType(),
						extBillType.getPurchaseBillId());

				for (int mark = 0; mark < financeType.size(); ++mark) {
					PayBill pb = (PayBill) financeType.get(mark);
					if (pb.getExamined().equals("N")) {
						throw new NotAllowException(payTypeName
								+ extBillType.getPurchaseBillCode()
								+ "有未审核的付款单，付款单号为" + pb.getBillCode()
								+ "，请进入【账务-付款单作废】，用付款单号" + pb.getBillCode()
								+ "查询找到这张付款单，先作废这张未审核的付款单，再重新开付款单。");
					}
				}
			}
		}

		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao,
					form.getCodeRule(), "", form.getPayBill().getTallySetId()));

			for (PayBill arg16 = payBillDao.get(obj.getBillCode()); arg16 != null; arg16 = payBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getPayBill().getTallySetId()));
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamSms("N");
			obj.setExamineTime(new Date());
		} else {
			obj.setExamSms("Y");
		}

		payBillDao.save(obj);
		boolean arg17 = false;
		if (obj.getExamined().equals("Y")) {
			arg17 = true;
		}

		this.saveItems(form, arg17, payBillItemDao, payBillDao, adjustBillDao,
				idGenerator);
		if (obj.getExamined().equals("Y")) {
			String arg18;
			String arg19;
			String arg20;
			if (obj.getPayType().equals("PURCHASE")) {
				arg18 = "采购";
				arg19 = "PURCHASE_BILL";
				arg20 = "PURCHASE";
			} else {
				if (!obj.getPayType().equals("COOPER")) {
					throw new Exception("pay type not support yet.");
				}

				arg18 = "外协";
				arg19 = "COOPER_BILL";
				arg20 = "COOPER";
			}

			String arg21 = form.getReceiverName();
			if (obj.getDescription() != null
					&& !obj.getDescription().equals("")) {
				arg21 = arg21 + "," + obj.getDescription();
			}

			PageHelper.updateBankAccount(obj.getBankAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getCost(),
					obj.getPayDate(), obj.getExamineTime(), obj.getBillCode(),
					"PAY_BILL", arg19, arg18, "P", obj.getManagerName(), arg21,
					arg20, "SAVE_EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
		}

		if (obj.getExamined().equals("Y")) {
			this.insertFinanceDetail(obj, payBillDao, idGenerator);
		}

	}

	private void insertFinanceDetail(PayBill obj, PayBillDao payBillDao,
			IdGenerator idGenerator) throws NotAllowException {
		FinanceDetail fd = new FinanceDetail();
		fd.setId(idGenerator.getId(FinanceDetail.class.getName()));
		fd.setBillDate(obj.getPayDate());
		fd.setBillId(obj.getId());
		fd.setBillType("PAY_BILL");
		fd.setRemCost((new BigDecimal(0)).subtract(obj.getCost()));
		fd.setRpCost(obj.getCost());
		fd.setEstId(obj.getReceiverId());
		fd.setFinanceType("P");
		fd.setTallySetId(obj.getTallySetId());
		payBillDao.save(fd);
	}

	public void txUpdate(PayBillBean form, PayBillDao payBillDao,
			PayBillItemDao payBillItemDao, IdGenerator idGenerator,
			AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao) throws Exception {
		PayBill obj = form.getPayBill();
		Object[] sv = payBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for pay bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("付款单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("付款单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("付款单已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamSms("N");
					obj.setExamineTime(new Date());
				} else {
					obj.setExamSms("Y");
				}

				obj.setUpdateTime(new Date());
				payBillDao.update(obj);
				boolean updateRelBillCost = false;
				if (obj.getExamined().equals("Y")) {
					updateRelBillCost = true;
				}

				this.saveItems(form, updateRelBillCost, payBillItemDao,
						payBillDao, adjustBillDao, idGenerator);
				if (obj.getExamined().equals("Y")) {
					String costType;
					String extBillType;
					String financeType;
					if (obj.getPayType().equals("PURCHASE")) {
						costType = "采购";
						extBillType = "PURCHASE_BILL";
						financeType = "PURCHASE";
					} else {
						if (!obj.getPayType().equals("COOPER")) {
							throw new Exception("pay type not support yet.");
						}

						costType = "外协";
						extBillType = "COOPER_BILL";
						financeType = "COOPER";
					}

					String mark = form.getReceiverName();
					if (obj.getDescription() != null
							&& !obj.getDescription().equals("")) {
						mark = mark + "," + obj.getDescription();
					}

					PageHelper.updateBankAccount(obj.getBankAccountId(),
							obj.getTallySetId(), obj.getId(), obj.getCost(),
							obj.getPayDate(), obj.getExamineTime(),
							obj.getBillCode(), "PAY_BILL", extBillType,
							costType, "P", obj.getManagerName(), mark,
							financeType, "SAVE_EXAM", false,
							(BankAccountDetailDao) null, bankAccountBalanceDao,
							idGenerator);
				}

				if (obj.getExamined().equals("Y")) {
					this.insertFinanceDetail(obj, payBillDao, idGenerator);
				}

			}
		}
	}

	private void saveItems(PayBillBean form, boolean updateRelBillCost,
			PayBillItemDao payBillItemDao, PayBillDao payBillDao,
			AdjustBillDao adjustBillDao, IdGenerator idGenerator)
			throws Exception {
		List items = form.getItems();
		int newNum = 0;

		for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
			PayBillItemBean bean = (PayBillItemBean) items.get(curItemId);
			if (bean.getStatus().equals("NEW")
					&& bean.getPayBillItem().getThisCost()
							.compareTo(new BigDecimal(0)) != 0) {
				++newNum;
			}
		}

		long arg16 = 0L;
		if (newNum > 0) {
			arg16 = idGenerator.getId(PayBillItem.class.getName(), newNum);
		}

		for (int i = 0; i < items.size(); ++i) {
			PayBillItemBean bean1 = (PayBillItemBean) items.get(i);
			if (bean1.getOldId() != 0L || !bean1.getStatus().equals("DELETE")) {
				String[] doActs;
				List iList;
				if (form.getPayBill().getPayType().equals("PURCHASE")) {
					PurchaseBill b = (PurchaseBill) payBillDao.get(
							PurchaseBill.class, bean1.getPayBillItem()
									.getPurchaseBillId());
					PurchaseBillExt be = (PurchaseBillExt) payBillDao.get(
							PurchaseBillExt.class, bean1.getPayBillItem()
									.getPurchaseBillId());
					if (b == null) {
						throw new NotAllowException("找不到id为"
								+ bean1.getPayBillItem().getPurchaseBillId()
								+ "的采购单");
					}

					doActs = this.saveItem(form, bean1, payBillItemDao, arg16);
					if (bean1.getStatus().equals("NEW")
							&& bean1.getPayBillItem().getThisCost()
									.compareTo(new BigDecimal(0)) != 0) {
						++arg16;
					}

					if (doActs[0].equals("Y")) {
						if (be.getExamined().equals("N")) {
							throw new NotAllowException("单号为" + b.getBillCode()
									+ "的采购单还没审核，不能操作");
						}

						if (be.getEnabled().equals("N")) {
							throw new NotAllowException("单号为" + b.getBillCode()
									+ "的采购单已作废，不能操作");
						}

						if (b.getProviderId() != form.getPayBill()
								.getReceiverId()) {
							throw new NotAllowException("不同供应商的采购单不能一起操作，单号："
									+ b.getBillCode());
						}

						if (b.getTallySetId() != form.getPayBill()
								.getTallySetId()) {
							throw new NotAllowException("采购单帐套与当前开单帐套不一致，单号："
									+ b.getBillCode());
						}
					}

					if (doActs[1].equals("Y")) {
						iList = payBillItemDao.getList(b.getId(), form
								.getPayBill().getPayType());
						if (iList.size() > 0) {
							be.setHasPay("Y");
						} else {
							be.setHasPay("N");
						}
					} else if (doActs[2].equals("Y")) {
						be.setHasPay("Y");
					}

					if (updateRelBillCost && doActs[0].equals("Y")
							&& doActs[1].equals("N")) {
						if (b.getBillType().equals("BUY")) {
							be.setPayCost(be.getPayCost().add(
									bean1.getPayBillItem().getThisCost()));
							be.setPaiedCost(be.getPaiedCost().add(
									bean1.getPayBillItem().getThisCost()));
						} else {
							be.setPayCost(be.getPayCost().subtract(
									bean1.getPayBillItem().getThisCost()));
							be.setPaiedCost(be.getPaiedCost().subtract(
									bean1.getPayBillItem().getThisCost()));
						}

						if (b.getCost().subtract(be.getPaiedCost())
								.add(be.getAdjustCost())
								.compareTo(new BigDecimal(0)) == 0) {
							be.setPaied("Y");
						} else {
							be.setPaied("N");
						}
					}

					payBillDao.update(be);
				} else {
					if (!form.getPayBill().getPayType().equals("COOPER")) {
						throw new Exception("pay type not support yet.");
					}

					CooperBill arg17 = (CooperBill) payBillDao.get(
							CooperBill.class, bean1.getPayBillItem()
									.getPurchaseBillId());
					CooperBillExt arg18 = (CooperBillExt) payBillDao.get(
							CooperBillExt.class, bean1.getPayBillItem()
									.getPurchaseBillId());
					if (arg17 == null) {
						throw new NotAllowException("找不到id为"
								+ bean1.getPayBillItem().getPurchaseBillId()
								+ "的外协单");
					}

					doActs = this.saveItem(form, bean1, payBillItemDao, arg16);
					if (bean1.getStatus().equals("NEW")
							&& bean1.getPayBillItem().getThisCost()
									.compareTo(new BigDecimal(0)) != 0) {
						++arg16;
					}

					if (doActs[0].equals("Y")) {
						if (arg18.getExamined().equals("N")) {
							throw new NotAllowException("单号为"
									+ arg17.getBillCode() + "的外协单还没审核，不能操作");
						}

						if (arg18.getEnabled().equals("N")) {
							throw new NotAllowException("单号为"
									+ arg17.getBillCode() + "的外协单已作废，不能操作");
						}

						if (arg17.getCooperatorId() != form.getPayBill()
								.getReceiverId()) {
							throw new NotAllowException("不同外协商的外协单不能一起操作，单号："
									+ arg17.getBillCode());
						}

						if (arg17.getTallySetId() != form.getPayBill()
								.getTallySetId()) {
							throw new NotAllowException("外协单帐套与当前开单帐套不一致，单号："
									+ arg17.getBillCode());
						}
					}

					if (doActs[1].equals("Y")) {
						iList = payBillItemDao.getList(arg17.getId(), form
								.getPayBill().getPayType());
						if (iList.size() > 0) {
							arg18.setHasPay("Y");
						} else {
							arg18.setHasPay("N");
						}
					} else if (doActs[2].equals("Y")) {
						arg18.setHasPay("Y");
					}

					if (updateRelBillCost && doActs[0].equals("Y")
							&& doActs[1].equals("N")) {
						arg18.setPayCost(arg18.getPayCost().add(
								bean1.getPayBillItem().getThisCost()));
						arg18.setPaiedCost(arg18.getPaiedCost().add(
								bean1.getPayBillItem().getThisCost()));
						if (arg17.getCost().subtract(arg18.getPaiedCost())
								.add(arg18.getAdjustCost())
								.compareTo(new BigDecimal(0)) == 0) {
							arg18.setPaied("Y");
						} else {
							arg18.setPaied("N");
						}
					}

					payBillDao.update(arg18);
				}
			}
		}

	}

	private String[] saveItem(PayBillBean form, PayBillItemBean bean,
			PayBillItemDao payBillItemDao, long newItemId) {
		String doDb = "N";
		String doDel = "N";
		String doIns = "N";
		bean.getPayBillItem().setBillId(form.getPayBill().getId());
		if (bean.getStatus().equals("DELETE")) {
			if (bean.getOldId() != 0L) {
				payBillItemDao.delete(bean.getOldId());
				doDb = "Y";
				doDel = "Y";
			}
		} else if (bean.getStatus().equals("EDIT")) {
			if (bean.getPayBillItem().getThisCost()
					.compareTo(new BigDecimal(0)) != 0) {
				payBillItemDao.update(bean.getPayBillItem());
				doDb = "Y";
			} else {
				payBillItemDao.delete(bean.getOldId());
				doDb = "Y";
				doDel = "Y";
			}
		} else if (bean.getStatus().equals("NEW")) {
			if (bean.getPayBillItem().getThisCost()
					.compareTo(new BigDecimal(0)) != 0) {
				bean.getPayBillItem().setId(newItemId);
				payBillItemDao.save(bean.getPayBillItem());
				doDb = "Y";
				doIns = "Y";
			}
		} else if (bean.getStatus().equals("UNCHANGE")) {
			doDb = "Y";
		}

		return new String[]{doDb, doDel, doIns};
	}

	public void txExamine(long id, PayBillDao payBillDao,
			PayBillItemDao payBillItemDao, AdjustBillDao adjustBillDao,
			long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao, IdGenerator idGenerator)
			throws Exception {
		PayBill a = (PayBill) payBillDao.get(PayBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			List itemList = payBillItemDao.getListByPayBill(a.getId());
			a.setExamined("Y");
			a.setExamSms("N");
			a.setExamineTime(new Date());
			a.setExaminerId(examinerId);
			a.setExaminerName(examinerName);
			a.setUpdateTime(new Date());
			payBillDao.update(a);
			String clientName = "";
			Establishment est = (Establishment) bankAccountBalanceDao.get(
					Establishment.class, a.getReceiverId());
			String costType;
			String extBillType;
			String financeType;
			if (a.getPayType().equals("PURCHASE")) {
				costType = "采购";
				extBillType = "PURCHASE_BILL";
				financeType = "PURCHASE";
			} else {
				if (!a.getPayType().equals("COOPER")) {
					throw new Exception("pay type not support yet.");
				}

				costType = "外协";
				extBillType = "COOPER_BILL";
				financeType = "COOPER";
			}

			if (est != null) {
				clientName = est.getEstName();
				if (a.getDescription() != null
						&& !a.getDescription().equals("")) {
					clientName = clientName + a.getDescription();
				}
			}

			this.updateRelBill(a.getPayType(), itemList, payBillDao,
					adjustBillDao, false, payBillItemDao, "ADD");
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(), a.getCost(), a.getPayDate(),
					a.getExamineTime(), a.getBillCode(), "PAY_BILL",
					extBillType, costType, "P", a.getManagerName(), clientName,
					financeType, "EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
			this.insertFinanceDetail(a, payBillDao, idGenerator);
		}

	}

	private void updateRelBill(String payType, List<?> itemList,
			PayBillDao payBillDao, AdjustBillDao adjustBillDao,
			boolean checkHasPay, PayBillItemDao payBillItemDao, String costType)
			throws Exception {
		for (int i = 0; i < itemList.size(); ++i) {
			PayBillItem item = (PayBillItem) itemList.get(i);
			List iList;
			if (payType.equals("PURCHASE")) {
				PurchaseBill b = (PurchaseBill) payBillDao.get(
						PurchaseBill.class, item.getPurchaseBillId());
				PurchaseBillExt be = (PurchaseBillExt) payBillDao.get(
						PurchaseBillExt.class, item.getPurchaseBillId());
				if (b == null) {
					throw new NotAllowException("找不到id为"
							+ item.getPurchaseBillId() + "的采购单");
				}

				if (be.getExamined().equals("N")) {
					throw new NotAllowException("单号为" + b.getBillCode()
							+ "的采购单还没审核，不能操作");
				}

				if (be.getEnabled().equals("N")) {
					throw new NotAllowException("单号为" + b.getBillCode()
							+ "的采购单已作废，不能操作");
				}

				if (checkHasPay) {
					iList = payBillItemDao.getList(b.getId(), payType);
					if (iList.size() > 0) {
						be.setHasPay("Y");
					} else {
						be.setHasPay("N");
					}
				}

				if (!costType.equals("NONE")) {
					if (costType.equals("ADD")) {
						if (b.getBillType().equals("BUY")) {
							be.setPayCost(be.getPayCost().add(
									item.getThisCost()));
							be.setPaiedCost(be.getPaiedCost().add(
									item.getThisCost()));
						} else {
							be.setPayCost(be.getPayCost().subtract(
									item.getThisCost()));
							be.setPaiedCost(be.getPaiedCost().subtract(
									item.getThisCost()));
						}
					} else if (b.getBillType().equals("BUY")) {
						be.setPayCost(be.getPayCost().subtract(
								item.getThisCost()));
						be.setPaiedCost(be.getPaiedCost().subtract(
								item.getThisCost()));
					} else {
						be.setPayCost(be.getPayCost().add(item.getThisCost()));
						be.setPaiedCost(be.getPaiedCost().add(
								item.getThisCost()));
					}

					if (b.getCost().subtract(be.getPaiedCost())
							.add(be.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						be.setPaied("Y");
					} else {
						be.setPaied("N");
					}
				}

				payBillDao.update(be);
			} else {
				if (!payType.equals("COOPER")) {
					throw new Exception("pay type not support yet.");
				}

				CooperBill arg12 = (CooperBill) payBillDao.get(
						CooperBill.class, item.getPurchaseBillId());
				CooperBillExt arg13 = (CooperBillExt) payBillDao.get(
						CooperBillExt.class, item.getPurchaseBillId());
				if (arg12 == null) {
					throw new NotAllowException("找不到id为"
							+ item.getPurchaseBillId() + "的外协单");
				}

				if (arg13.getExamined().equals("N")) {
					throw new NotAllowException("单号为" + arg12.getBillCode()
							+ "的外协单还没审核，不能操作");
				}

				if (arg13.getEnabled().equals("N")) {
					throw new NotAllowException("单号为" + arg12.getBillCode()
							+ "的外协单已作废，不能操作");
				}

				if (checkHasPay) {
					iList = payBillItemDao.getList(arg12.getId(), payType);
					if (iList.size() > 0) {
						arg13.setHasPay("Y");
					} else {
						arg13.setHasPay("N");
					}
				}

				if (!costType.equals("NONE")) {
					if (costType.equals("ADD")) {
						arg13.setPayCost(arg13.getPayCost().add(
								item.getThisCost()));
						arg13.setPaiedCost(arg13.getPaiedCost().add(
								item.getThisCost()));
					} else {
						arg13.setPayCost(arg13.getPayCost().subtract(
								item.getThisCost()));
						arg13.setPaiedCost(arg13.getPaiedCost().subtract(
								item.getThisCost()));
					}

					if (arg12.getCost().subtract(arg13.getPaiedCost())
							.add(arg13.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						arg13.setPaied("Y");
					} else {
						arg13.setPaied("N");
					}
				}

				payBillDao.update(arg13);
			}
		}

	}

	public void txUnExamine(long id, PayBillDao payBillDao,
			PayBillItemDao payBillItemDao, AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			FinanceDetailDao financeDetailDao, IdGenerator idGenerator,
			UserBean ub) throws Exception {
		PayBill a = (PayBill) payBillDao.get(PayBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExamSms("Y");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			payBillDao.update(a);
			List itemList = payBillItemDao.getListByPayBill(a.getId());
			this.updateRelBill(a.getPayType(), itemList, payBillDao,
					adjustBillDao, false, payBillItemDao, "SUB");
			String costType;
			String extBillType;
			String financeType;
			if (a.getPayType().equals("PURCHASE")) {
				costType = "采购";
				extBillType = "PURCHASE_BILL";
				financeType = "PURCHASE";
			} else {
				if (!a.getPayType().equals("COOPER")) {
					throw new Exception("pay type not support yet.");
				}

				costType = "外协";
				extBillType = "COOPER_BILL";
				financeType = "COOPER";
			}

			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(),
					(new BigDecimal(0)).subtract(a.getCost()), a.getPayDate(),
					new Date(), a.getBillCode(), "PAY_BILL", extBillType,
					costType, "P", a.getManagerName(), mark, financeType,
					"UN_EXAM", true, bankAccountDetailDao,
					bankAccountBalanceDao, idGenerator);
			financeDetailDao.delete(a.getId(), "PAY_BILL");
		}

	}

	public void txCancel(long id, String reason, PayBillDao payBillDao,
			PayBillItemDao payBillItemDao, AdjustBillDao adjustBillDao,
			UserBean ub, BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			FinanceDetailDao financeDetailDao, IdGenerator idGenerator)
			throws Exception {
		PayBill d = (PayBill) payBillDao.get(PayBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setExamSms("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			payBillDao.update(d);
			List itemList = payBillItemDao.getListByPayBill(d.getId());
			String costType = "NONE";
			if (d.getExamined().equals("Y")) {
				costType = "SUB";
			}

			this.updateRelBill(d.getPayType(), itemList, payBillDao,
					adjustBillDao, true, payBillItemDao, costType);
			if (d.getExamined().equals("Y")) {
				String log;
				String financeType;
				if (d.getPayType().equals("PURCHASE")) {
					costType = "采购";
					log = "PURCHASE_BILL";
					financeType = "PURCHASE";
				} else {
					if (!d.getPayType().equals("COOPER")) {
						throw new Exception("pay type not support yet.");
					}

					costType = "外协";
					log = "COOPER_BILL";
					financeType = "COOPER";
				}

				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String mark = ub.getStaffer().getStafferName() + "于"
						+ sf.format(new Date()) + "作废单据";
				PageHelper.updateBankAccount(d.getBankAccountId(),
						d.getTallySetId(), d.getId(),
						(new BigDecimal(0)).subtract(d.getCost()),
						d.getPayDate(), new Date(), d.getBillCode(),
						"PAY_BILL", log, costType, "P", d.getManagerName(),
						mark, financeType, "CANC", true, bankAccountDetailDao,
						bankAccountBalanceDao, idGenerator);
			}

			if (d.getExamined().equals("Y")) {
				financeDetailDao.delete(d.getId(), "PAY_BILL");
			}

			SystemLog log1 = new SystemLog();
			log1.setId(idGenerator.getId(SystemLog.class.getName()));
			log1.setActionCode("PAY_BILL");
			log1.setLogType("CANCEL_LOG");
			log1.setCreaterId(ub.getStaffer().getId());
			log1.setCreaterName(ub.getStaffer().getStafferName());
			log1.setDepName(ub.getDepName());
			log1.setDepId(ub.getStaffer().getDepartmentId());
			log1.setStatus("S");
			log1.setUserName(ub.getUser().getUserName());
			log1.setIp(d.getBillCode());
			log1.setMark("");
			log1.setRelId(id);
			log1.setCancelReason(reason);
			log1.setTallySetId(d.getTallySetId());
			payBillDao.save(log1);
		}

	}
}